package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

//ReadFile函数读取整个文件,把全部输入数据读到内存中,一次分割为多行
func main() {
	counts := make(map[string]int)
	for _, filename := range os.Args[1:] {
		data, err := ioutil.ReadFile(filename)
		if err != nil {
			fmt.Fprintf(os.Stderr, "dup3: %v\n", err)
			continue
		}
		//如果只用换行符切割,还有\r字符保留,这样最后一行数据和前面的会不一样,少了一个\r
		for _, line := range strings.Split(string(data), "\r\n") {
			counts[line]++
		}
	}
	for line, n := range counts {
		if n > 0 {
			fmt.Printf("%d\t%s\n", n, line)
		}
	}

}
